//
//  AppDelegate.m
//  LPTClient
//
//  Created by mac on 2018/12/2.
//  Copyright © 2018年 zhaodd. All rights reserved.
//

#import "AppDelegate.h"
#import "LPTTabBarViewController.h"
#import "LPTBaseRequest.h"
#import "ArticleListModel.h"
#import "MMProgressHUD.h"
#import "UINavigationController+Category.h"
#import "AFNetworking.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    

    [self loadMainVC];
    
    [self setUpModuleViews];
    //[self requestUploadImage:[UIImage imageNamed:@"bg_data"]];
    return YES;
}


- (void)requestUploadImage:(UIImage *)image
{
    //1.创建管理者对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    
    NSString *token = [LTPCacheManager shareInstance].token;
    
    token = [NSString stringWithFormat:@"Bearer %@",token];

    [manager.requestSerializer setValue:token forHTTPHeaderField:@"authorization"];
    
    NSString *uploadUrl = [NSString stringWithFormat:@"%@%@", BASE_URL, @"/common/file/upload"];
    [manager POST:uploadUrl parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"bg_data"], 1);
        
        [formData appendPartWithFileData:imageData name:@"file" fileName:@"file.jpg" mimeType:@"image/jpeg"];
        
    } progress:^(NSProgress * _Nonnull uploadProgress) {
        //打印上传进度
        CGFloat progress = 100.0 * uploadProgress.completedUnitCount / uploadProgress.totalUnitCount;
        NSLog(@"%.2lf%%", progress);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject
                                                            options:NSJSONReadingMutableContainers
                                                              error:nil];
        ImageInfo *imgModel = [ImageInfo modelWithDictionary:dic[@"data"][@"file"]];
        NSLog(@"请求成功：%@",responseObject);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        //请求失败
        NSLog(@"请求失败：%@",error);
        
    }];
}

- (void)loadMainVC
{
    LPTTabBarViewController *tabbarVC = [LPTTabBarViewController instantiate];
    self.window.rootViewController = tabbarVC;

}

- (void)setUpModuleViews
{
    [MMProgressHUD setPresentationStyle:MMProgressHUDPresentationStyleNone];
    
//    [UINavigationBar appearance].backIndicatorTransitionMaskImage = [UIImage imageNamed:@"nav_icon_return"];
    [UINavigationBar appearance].backIndicatorImage = [UIImage imageNamed:@"nav_icon_return"];
 
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
